class Scope:
    allow_api = []
    allow_module = []
    forbidden = []

    def __add__(self, other):
        self.allow_api = self.allow_api + other.allow_api
        self.allow_api = list(set(self.allow_api))
        # 运算符重载

        self.allow_module = self.allow_module + \
                            other.allow_module
        self.allow_module = list(set(self.allow_module))

        self.forbidden = self.forbidden + other.forbidden
        self.forbidden = list(set(self.forbidden))

        return self


class AdminScope(Scope):
    allow_module = ['v1.admin/shop', 'v1.admin/alipay', 'v1.admin/moderator', 'v1/admin/user']
    allow_api = ['v1.admin/moderator+moderator_upgrade', 'v1.admin/moderator+moderator_downgrade', \
                 'v1.admin/user+super_get_user', 'v1.admin/user+super_delete_user']

    def __init__(self):
        # self + ModeratorPlusScope
        self + ModeratorScope


class ModeratorPlusScope(Scope):
    allow_module = ['v1.admin/moderator']
    allow_api = ['v1.admin/user+super_get_user', 'v1.admin/user+super_delete_user']
    forbidden = ['v1.admin/moderator+moderator_upgrade', 'v1.admin/moderator+moderator_downgrade']

    def __init__(self):
        self + ModeratorScope()


class ModeratorScope(Scope):
    allow_module = ['v1.admin/writer', 'v1.admin/tasks', 'v1.admin/tb_orders', \
                    'v1.admin/user', 'v1.admin/dashboard', 'v1.admin/desktop', 'v1.admin/oss', 'v1.admin/notices',
                    'v1.admin/custom', 'v1.admin/chat']
    allow_api = ['v1.admin/moderator+achievement', 'v1.admin/moderator+settle_tasks', \
                 'v1.admin/moderator+settle_tb_orders', 'v1.admin/shop+get_shops', 'v1.admin/shop+get_moderator_shops']

    forbidden = ['v1.admin/user+super_get_user',
                 'v1.admin/user+super_delete_user']


class WriterScope(Scope):
    allow_module = ['v1.writer/tasks', 'v1.writer/dashboard', 'v1.writer/user', 'v1.writer/paylog', 'v1.writer/alipay', \
                    'v1.wx/navigation', 'v1.wx/tasks', 'v1.wx/amount', 'v1.wx/bill', 'v1.wx/dashboard', 'v1.wx/im',
                    'v1.wx/alipay', 'v1.wx/oss', 'v1.wx/chat','v1.wxpay']


class WxMpScope(Scope):
    allow_module = ['v1.wx_article', 'v1.user', 'v1.report', 'v1.article', 'v1.wxpay','v1.wx_aigc_check']


class CustomScope(Scope):

    def __init__(self):
        self + WriterScope()

    allow_module = ['v1.wx/orders', 'v1.wx/chat']

    # allow_api = ['v1.user+get_user', 'v1.user+delete_user']


def is_in_scope(scope, endpoint):
    # scope()
    # 反射
    # globals
    # v1.view_func   v1.module_name+view_func
    # v1.red_name+view_func
    scope = globals()[scope]()
    splits = endpoint.split('+')
    red_name = splits[0]
    if endpoint in scope.allow_api:
        return True
    if endpoint in scope.forbidden:
        return False
    if red_name in scope.allow_module:
        return True
    else:
        return False
